import requests
import json
import os
import time
import random
import datetime

class Guatian(object):
    save_dir = './'
    token_url = 'http://guatian.jy-tech.com.cn/rest/user/token'
    info_url = 'http://guatian.jy-tech.com.cn/rest/questions/all-communication?currentPage={}&pageSize=20'
    
    token_headers = {
        'token': '',
        'api_version': '17',
        'Content-Type': 'application/json; charset=UTF-8',
        'Content-Length': '58',
        'Host': 'guatian.jy-tech.com.cn',
        'Connection': 'Keep-Alive',
        'Accept-Encoding': 'gzip',
        'Cookie': 'token: ' + '',
        'User-Agent': 'okhttp/2.5.0',
    }
    token_body = json.dumps({'id': 0, 'refreshToken': '8cee816450684edaa2cfb755a2cce5f2'})

    info_headers = {
        'Content-Type': 'application/json',
        'token': '',
        'api_version': '17',
        'Host': 'guatian.jy-tech.com.cn',
        'Connection': 'Keep-Alive',
        'Accept-Encoding': 'gzip',
        'Cookie': 'token={}',
        'User-Agent': 'okhttp/2.5.0'
    }

    def __init__(self):
        self.folder_exist(Guatian.save_dir)
        self.token = ''
        # 获取第一个token值
        self.token = self.get_token()
    
    def get_token(self, token=''):
        ''' 获取token值 '''
        Guatian.token_headers['token'] = self.token
        Guatian.token_headers['Cookie'] = 'token: ' + self.token
        response = requests.post(url=Guatian.token_url, headers=Guatian.token_headers, data=Guatian.token_body)
        response.endcoding = 'utf-8'
        ret = json.loads(response.text)
        return ret['payload']['accessToken']
    
    def request_json(self, token, i):
        ''' 请求json数据 '''
        url = Guatian.info_url.format(i)
        Guatian.info_headers['token'] = token
        Guatian.info_headers['Cookie'] = 'token=' + token

        print(Guatian.info_headers['token'], url, Guatian.info_headers['Cookie'])

        response = requests.get(url=url, headers=Guatian.info_headers)
        response.endcoding = 'utf-8'
        # print(response.text)
        self.token = self.get_token(token)
    
        return response.text
    
    def download_all(self):
        # 爬取前4页
        for i in range(1, 5):
            json_content = self.request_json(self.token, i)
            with open('page_'+str(i), 'w') as f:
                f.write(json_content)
            time.sleep(random.randint(2, 10)) # 随机暂停2-10秒

    def folder_exist(self, dir_path):
        '''
        1. 作用:判断文件夹路径是否存在,不存在则创建
        2. 参数:dir_path:文件夹路径
        3. 返回值:None
        '''
        if not os.path.exists(dir_path):
            os.makedirs(dir_path)
    
    def get_yesterday(self):
        '''
        1. 作用:获取前一天的年月日字符串
        2. 参数:无
        3. 返回值:前一天的年月日
        '''
        today_time = datetime.datetime.today()
        yesterday = today_time + datetime.timedelta(-1)
        str_yesterday = datetime.datetime.strftime(yesterday, "%Y%m%d")
        return str_yesterday

# # 调用接口
# def post_guatian_data(data):
# 	try:
# 		r = api.post_guatian_data(data)
# 		return (json.loads(r)['data'])
# 	except Exception as e:
# 		log.error('api request fail: %s'%(e), exc_info=True)

# def deal_guatian_info(list_data):
# 	for data in list_data:
# 		id = data['id']
# 		content = json.dumps(data)
# 		data = {'id': id, 'content':content}
# 		print(data['id'], json.loads(data['content'])['pictures'][0])
# 		post_guatian_data(data)




# def save_json(json_data, i):
#     ''' 保存json文件 '''
#     today = datetime.datetime.today()
#     str_today = datetime.datetime.strftime(today, '%Y%m%d')
#     json_dir = base_dir.format(str_today)
#     folder_exist(json_dir)
#     json_path = os.path.join(json_dir, 'page_{}.json'.format(i))
#     with open(json_path.format(str_today, i), 'w', encoding='utf-8') as f:
#         print(json_path.format(str_today, i))
#         f.write(json_data)
#     return "{}:第{}页json存储成功".format(json_dir, i)

# def get_yesterday():
#     '''
#     1. 作用:获取前一天的年月日字符串
#     2. 参数:无
#     3. 返回值:前一天的年月日
#     '''
#     today_time = datetime.datetime.today()
#     yesterday = today_time + datetime.timedelta(-1)
#     str_yesterday = datetime.datetime.strftime(yesterday, "%Y%m%d")
#     return str_yesterday

# def list_file(path, ext='.json'):
#     '''
#     1.作用:遍历文件夹下的所有指定后缀名的文件的具体路径
#     2.参数： 
#         path:需要遍历的目录， 
#         ext:文件后缀名，默认为'.jpg'
#     3.返回值:文件的绝对路径
#     '''
#     list_file=[]
#     for root, dirs, files in os.walk(path):
#         for file in files:
#             file_ext = file.endswith(ext)
#             if file_ext:
#                 fpath = os.path.join(root, file)
#                 list_file.append(fpath)
#     return list_file

# def get_first_file(dir_path):
#     ''' 获取第一个一个文件 '''
#     list_filename = list_file(dir_path)
#     list_filename.sort()
#     return list_filename[0]

# def get_json_info(json_file):
#     ''' 通过json文件名获取内容 '''
#     with open(json_file) as f:
#         json_info = json.load(f)
#     return json_info

# if __name__ == '__main__':
#     token_value = '' # 初始的token值
#     base_dir = '/home/ubuntu/workspace/spider_data/guatian/json_data{}' # 保存数据的目录

#     for i in range(100):
#         token_value = token_one(token_value)
#         json_info = request_json(token_value, i)

#         str_yesterday = get_yesterday() # 昨天的年月日
#         print(str_yesterday)
#         dir_yesterday = base_dir.format(str_yesterday) # 昨天下载json文件的目录
#         last_file = get_first_file(dir_yesterday) # 第一个文件
#         yesterday_json_info = get_json_info(last_file) # 第一个文件的json信息
#         yesterday_id = yesterday_json_info['payload']['list'][0]['id'] # 昨天最新的id

#         dict_info = json.loads(json_info)
#         deal_guatian_info(dict_info['payload']['list']) # 向接口发送数据

#         result = save_json(json_info, i) # 保存文件
#         print(result)
#         # 判断昨天的id是否大于现在爬取的id,如果是那么已经拿到最新的数据了停止请求数据
#         if yesterday_id > dict_info['payload']['list'][-1]['id']:
#             break
#         time.sleep(random.randint(2, 10)) # 随机暂停2-10秒

guatian = Guatian()
guatian.download_all()